package dao;

import hbt.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import Dominio.FormaPago;

public class GeneralDAO {
	private static GeneralDAO instancia = null;
	private static SessionFactory sf = null;
	private Session session = null;
	
	public static GeneralDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new GeneralDAO();
		} 
		return instancia;
	}
	
	private Session getSession(){
		if(session == null || !session.isOpen())
			session = sf.openSession();
		return session;
	}
	
	public void closeSession(){
		if (session.isOpen()) session.close();
	}
	
	public FormaPago getFormaPagoByTipo(String tipo){
		Session s=getSession();
		s.beginTransaction();
		FormaPago formaPago = (FormaPago)session
								.createQuery("from FormaPago where tipo = :tipo ")
								.setParameter("tipo", tipo)
								.uniqueResult();
		s.getTransaction().commit();
		return formaPago;
	}
	
	public void persistFormaPago(FormaPago formaPago){
		Session s=getSession();
		s.beginTransaction();
		s.persist(formaPago);
		s.flush();
		s.getTransaction().commit();
	}
}
